package my2Cents.properties;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class CheckBox extends Property {
	private static final long serialVersionUID = 1L;
	private JCheckBox check;
	
	public CheckBox(String label, boolean init) {
		this.setSize(10,25);
		check = new JCheckBox();
		check.setText(label);
		check.setLocation(5,0);
		check.setBackground(bgColor);
		check.setSelected(init);
		add(check);
		centerY(check);
	    ActionListener checkBoxListener = new ActionListener() {
	        public void actionPerformed(ActionEvent actionEvent) {
	          AbstractButton abstractButton = (AbstractButton)actionEvent.getSource();
	          boolean selected = abstractButton.getModel().isSelected();
	          valueChanged(selected);
	        }
	    };
	    check.addActionListener(checkBoxListener);
	}
	
	public void setCloseKeyAdapter(KeyAdapter ka) {
		super.setCloseKeyAdapter(ka);
		check.addKeyListener(getCloseKeyAdapter());
	}
	
	public void validate() {
		super.validate();
		if(getParent() != null) {
			int w = getParent().getWidth();
			this.setSize(w,30);
			check.setSize(w-10,20);
			centerY(check);
		}
	}
	
	public void setValue(boolean v) {
		check.setSelected(v);
		valueChanged(v);
	}
	
	public void valueChanged(boolean value) {
		
	}
	
}
